<?php

namespace app\admin\controller;


use app\common\model\ShopMember;
use app\common\service\Status;
use app\common\model\ShopMemberMarketing;
use buildView\Button;
use buildView\Detail;
use buildView\Filter;
use think\Controller;
use buildView\Form;
use buildView\Grid;
use app\admin\service\Echarts;
use app\common\model\User;
use buildView\Excel;
use think\Db;

/**
 * 营销报表
 * Class MemberMarketing
 * @package app\admin\controller
 */
class MemberMarketing extends Controller
{

    /**
     * 数据
     */
    public function indeData()
    {
        $store = session('store');
        if ($store){
            $store_id = $store['id'];
        }else{
            $store_id = $this->request->param('store_id');
        }
        $echart = new Echarts();
        $echart->create('shop_order', '账单统计', '', 'line')
            ->setHeader('消费总额', 'sum', 'total_price', function ($q)use($store_id) {
                if ($store_id){
                    $q->where('status', 'neq',-1)->where('is_pay',1)->where('store_id',$store_id);
                }else{
                    $q->where('status', 'neq',-1)->where('is_pay',1);
                }
            })->sum('消费总额', 'total_price', function ($q)use($store_id) {
                if ($store_id){
                    $q->where('status', 'neq',-1)->where('is_pay',1)->where('store_id',$store_id);
                }else{
                    $q->where('status', 'neq',-1)->where('is_pay',1);
                }
        });
        $echart->create('shop_order', '微信支付', '', 'line')
            ->setHeader("微信支付", 'sum', 'total_price',function ($q) use ($store_id) {
                if ($store_id){
                    $q->where('status', 'neq',-1)->where('pay_type',1)->where('is_pay',1)->where('store_id',$store_id);
                }else{
                    $q->where('status', 'neq',-1)->where('pay_type',1)->where('is_pay',1);
                }
        })->sum('微信支付', 'total_price', function ($q)use($store_id) {
            if ($store_id){
                $q->where('status', 'neq',-1)->where('pay_type',1)->where('is_pay',1)->where('store_id',$store_id);
            }else{
                $q->where('status', 'neq',-1)->where('pay_type',1)->where('is_pay',1);
            }
        });
        $echart->create('shop_order', '实际支付总额', '', 'line')
            ->setHeader("实际支付总额", 'sum', 'pay_price',function ($q) use ($store_id) {
                if ($store_id){
                    $q->where('status', 'neq',-1)->where('is_pay',1)->where('store_id',$store_id);
                }else{
                    $q->where('status', 'neq',-1)->where('is_pay',1);
                }
        })->sum('实际支付总额', 'pay_price', function ($q)use($store_id) {
            if ($store_id){
                $q->where('status', 'neq',-1)->where('is_pay',1)->where('store_id',$store_id);
            }else{
                $q->where('status', 'neq',-1)->where('is_pay',1);
            }
        });
        $button = new Button('打印');
        $echart->addTools($button);

        return $echart->view();    
    }


    /**
     * 列表
     * @auth true
     * @menu true
     * @return string
     */
    public function index()
    {
        $grid = new Grid(new ShopMemberMarketing());
        $grid->consumption_total('销售总额');
        $grid->wechat_total('微信支付');
        $grid->real_total('实际支付');
        $grid->note('备注');
        $grid->create_at('创建时间');
        $grid->setFromOpen();
        $grid->hideAddButton();
        $grid->hideDeletesButton();
        // $grid->hideExportButton();
        $grid->filter(function(Filter $filter){
           $filter->dateBetween('time','时间');
           $filter->like('consumption_total','销售总额');
           $filter->like('wechat_total','微信支付');
           $filter->like('real_total','实际支付');
           $filter->dateBetween('create_at','创建时间');
        });
        $grid->actions(function($action,$data){
            $action->hideDel();
            $action->hideDetail();
        });
        return $grid->view();
    }

    /**
     * 添加/编辑
     * @auth true
     * @menu true
     * @return string
     */
    public function form()
    {
        $form = new Form(new ShopMemberMarketing);
		$form->text('consumption_total','销售总额')->required();
		$form->text('wechat_total','微信支付')->required();
		$form->text('real_total','实际支付')->required();
		$form->text('note','备注')->required();
        $form->saved(function($post,$model){
            (new ShopMemberMarketing)->saveAll($post);
            return $post;
        });
        return $form->view();
    }

    /**
     * 详情
     * @auth true
     * @menu true
     * @return string
     */
    public function detail()
    {
        $detail = new Detail(new ShopMemberMarketing);
        $detail->setTitle('会员等级信息');
        $detail->layout('基本信息', 12, function ($detail) {
            $detail->level_name('等级名称');
            $detail->discount('折扣');
        });
        return $detail->view();
    }


    
    
}
